<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>角色绑定</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/css.css"/>
</head>
<body>

<div class="layui-container" id="rolebind">
    <div class="layui-btn-group">
        <button class="layui-btn" id="bindsure">确认绑定</button>
    </div>
    <table id="rbind" lay-filter="rbind"></table>
</div>

</body>
<script src="layui/layui.js"></script>
<script src="js/common.js" charset="utf-8"></script>
<script>
    function roleBind(rowData) {
        layui.use(['jquery', 'layer', 'table'], function () {
            var layer = layui.layer,
                table = layui.table,
                $ = layui.jquery;

            var userId = rowData.id;

            //确认绑定
            $("#bindsure").on("click", function () {
                var checkStatus = table.checkStatus('rbind'); //test即为基础参数id对应的值
                var checkData = checkStatus.data;//获取选中行的数据

                if (checkData.length < 1) {
                    layer.msg('请先完成角色选择!');
                    return;
                }

                //提交到后台的数据
                var roleIdArr = [];

                //数据整理，生成角色id数组
                $.each(checkData, function (index, value) {
                    roleIdArr.push(value.id)
                });

                //发送绑定请求(发送数组时 +'' 转换为字符串)
                doAjax($, layer, 'POST', false, {
                    'userId': userId,
                    'roleIds': roleIdArr + ''
                }, '/user/roleBinding', afterBinding);

                //绑定成功回调函数
                function afterBinding(result) {
                    if (result.status == "200") {
                        //编辑成功的提示与跳转
                        layer.msg('绑定成功', {
                            icon: 1
                            , time: 1500
                        }, function () {
                            //iframe页面关闭自身
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            //刷新父级数据列表
                            window.parent.location.reload();
                        });

                    } else {
                        layer.msg(result.msg);
                    }
                }

            })
        });
    }

    layui.use(['table', 'jquery', 'form'], function () {
        var table = layui.table,
            $ = layui.jquery;

        table.render({
            elem: '#rbind'
            , url: 'role/list'
            , page: true
            , limit: 10
            , method: "get"
            , cols: [
                [
                    {type: 'checkbox'}
                    , {field: 'name', align: 'center', title: '角色名'}
                    , {field: 'note', align: 'center', title: '备注'}
                    , {field: 'createTime', align: 'center', title: '创建时间'}

                ]
            ]
        });

    });
</script>
</html>
